package com.lottery.ruok.lottrey.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.chat.event.manager.UnReadMessageManager;
import com.lottery.ruok.lottrey.retrofitModule.response.GetRoomAdminResponse;

import de.hdodenhof.circleimageview.CircleImageView;
import glideHelp.GlideHelper;
import io.rong.imlib.RongIMClient;

/**
 * Created by ASUS on 2017/11/6.
 */

public class ChoosemanagerAdapter extends BaseAdapter {
    private Context mContext;
    private GetRoomAdminResponse getRuleResponses;

    public ChoosemanagerAdapter(Context context, GetRoomAdminResponse getRuleResponses) {
        mContext = context;
        this.getRuleResponses = getRuleResponses;
    }

    @Override
    public int getCount() {
        return getRuleResponses.getList().size();
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public Object getItem(int i) {
        return getRuleResponses.getList().get(i);
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (view != null) {
            holder = (ViewHolder) view.getTag();
        } else {
            view = LayoutInflater.from(mContext).inflate(R.layout.choose_manager_child_item, null);
            holder = new ViewHolder();
            holder.name = view.findViewById(R.id.name);
            holder.avatar = view.findViewById(R.id.avatar);
            holder.bubble = view.findViewById(R.id.tv_bubble);

            view.setTag(holder);
        }
        GetRoomAdminResponse.ListBean listBean = getRuleResponses.getList().get(i);
        holder.name.setText(listBean.getNickname());
        GlideHelper.loadImage(mContext, listBean.getAvatar(), holder.avatar, R.mipmap.chat_default);
        UnReadMessageManager.getInstance().getPrivateUnreadCount(new RongIMClient.ResultCallback<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                if (integer > 0) {
                    holder.bubble.setVisibility(View.VISIBLE);
                    holder.bubble.setText(integer + "");
                } else {
                    holder.bubble.setVisibility(View.GONE);
                }
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {

            }
        }, listBean.getRy_id());

        return view;
    }

    static class ViewHolder {
        TextView name;
        CircleImageView avatar;
        TextView bubble;
    }
}
